<template>

  <div>
    <div class="recharge-hd">
      <span>实名认证</span>
      <el-button size="mini" v-if="myInfo.isReal==2 && !dialogRealyVisible" :disabled="disabled" @click="realproves(true)" type="danger">修改信息</el-button>
      <el-button size="mini" v-if="myInfo.isReal==2 && dialogRealyVisible" :disabled="disabled" @click="realproves(false)" type="danger">取消修改</el-button>
    </div>
    <el-form v-if="myInfo.isReal==0 || dialogRealyVisible" :rules="rules" ref="ruleForm" :model="ruleForm" label-width="150px" v-loading='loading'>
      <div class="relboxmain">
        <div class="relbox">
          <el-form-item label="身份证正面" prop="cardImg" v-if="myInfo.isReal==2 || myInfo.isReal==0">
            <el-upload :headers="headers" :data="{'types':1}" name="positive" :limit="1" list-type="picture-card" :file-list="sfzqmList" :on-error="sfzqmError" :on-remove="sfzqmRemove" :action="uploadPath" :on-success="sfzqmSuccess" :before-upload="beforeAvatarUpload">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="身份证反面" prop="cardBackImg" v-if="myInfo.isReal==2 || myInfo.isReal==0">
            <el-upload :headers="headers" :data="{'types':2}" name="positive" :limit="1" list-type="picture-card" :file-list="sfzhmList" :on-error="sfzhmError" :on-remove="sfzhmRemove" :action="uploadPath" :on-success="sfzhmSuccess" :before-upload="beforeAvatarUpload">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="姓名：" prop="userName" v-if="myInfo.isReal==2 || myInfo.isReal==0">
            <el-input readonly :value="ruleForm.userName" />
          </el-form-item>
          <el-form-item label="身份证号码：" prop="userCardNum" v-if="myInfo.isReal==2 || myInfo.isReal==0">
            <el-input readonly :value="ruleForm.userCardNum" />
          </el-form-item>
          <el-form-item label="选择银行：" prop="bankCode">
            <el-select v-model="ruleForm.bankCode" filterable placeholder="请选择">
              <el-option v-for="item in bankListL" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开户行省：" prop="bankProvince">
            <el-select v-model="ruleForm.bankProvince" @change="bindProvince" filterable placeholder="请选择">
              <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开户行市：" prop="bankCity">
            <el-select v-model="ruleForm.bankCity" filterable placeholder="请选择">
              <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="银行卡号：" prop="bankAccount">
            <el-input v-model="ruleForm.bankAccount" />
          </el-form-item>
          <el-form-item label="开户支行：" prop="bankSubname">
            <el-input v-model="ruleForm.bankSubname" />
          </el-form-item>
          <el-form-item label="提现密码：" prop="bankPass">
            <el-input v-model="ruleForm.bankPass" />
          </el-form-item>
        </div>

      </div>

      <el-form-item>
        <el-button v-if="!dialogRealyVisible" :loading='loading' @click="handlePublish" type="danger">保存</el-button>
        <el-button v-if="dialogRealyVisible" :loading='loading' @click="handleAmend" type="danger">确定修改</el-button>
      </el-form-item>
    </el-form>
    <div v-if="myInfo.isReal==1" class="myrealy">实名认证成功</div>
    <div v-if="myInfo.isReal==2&&!dialogRealyVisible" class="myrealy">审核未通过</div>
    <div v-if="myInfo.isReal==3" class="myrealy">资料以及提交审核中.....</div>
  </div>
</template>

<script>
export default {
  name: 'realName',
  data () {
    return {
      dialogRealyVisible: false,
      loading: false,
      headers: localStorage.getItem('toke'),
      uploadPath: this.$apis.uploadPath,
      sfzqmList: [],
      sfzhmList: [],
      provinceList: [], // 省
      cityList: [], // 市
      cityListOld: [],
      bankListL: [
        { id: 1, name: '工商银行' },
        { id: 3, name: '农业银行' },
        { id: 2, name: '建设银行' },
        { id: 5, name: '中国银行' },
        { id: 7, name: '招商银行' },
        { id: 33, name: '北京银行' },
        { id: 6, name: '交通银行' },
        { id: 13, name: '兴业银行' },
        { id: 65, name: '南京银行' },
        { id: 14, name: '民生银行' },
        { id: 8, name: '光大银行' },
        { id: 18, name: '平安银行' },
        { id: 67, name: '渤海银行' },
        { id: 66, name: '东亚银行' },
        { id: 17, name: '宁波银行' },
        { id: 12, name: '中信银行' },
        { id: 11, name: '广发银行' },
        { id: 16, name: '上海银行' },
        { id: 9, name: '上海浦东发展银行' },
        { id: 4, name: '中国邮政' },
        { id: 10, name: '华夏银行' },
        { id: 68, name: '北京农村商业银行' }
      ],
      rules: {
        cardImg: [
          { required: true, message: '请选择身份证正面', trigger: 'change' }
        ],
        cardBackImg: [
          { required: true, message: '请选择身份证反面', trigger: 'change' }
        ],
        bankCode: [
          { required: true, message: '请选择银行', trigger: 'change' }
        ],
        bankAccount: [
          { required: true, message: '请填写银行卡号', trigger: 'blur' }
        ],
        bankProvince: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        bankCity: [{ required: true, message: '请选择市', trigger: 'change' }],
        bankSubname: [
          { required: true, message: '请填写开户支行', trigger: 'blur' }
        ],
        bankPass: [
          { required: true, message: '请填写提现密码', trigger: 'blur' }
        ]
      },

      ruleForm: {
        birthday: '',
        cardImg: '',
        cardBackImg: '',
        companyCode: '',
        companyName: '',
        companyUrl: '',
        gender: '',
        postalAddress: '',
        userCardNum: '',
        userName: '',
        yyzz: '',
        bankCode: '', // 银行代码
        bankAccount: '', // 开户账号
        bankProvince: '', // 开户行省
        bankCity: '', // 开户行市
        bankSubname: '', // 开户支行
        bankPass: '' // 提现密码
      }
    }
  },
  props: ['myInfo'],
  computed: {},

  mounted () {
    this.init()
  },
  methods: {
    init () {
      if (this.provinceList.length <= 0 && localStorage.getItem('token')) {
        this.$apis
          .province({
            id: 0
          })
          .then(res => {
            this.provinceList = res.data.data.list
          })
        this.$apis.city().then(res => {
          this.cityListOld = res.data.data.list
        })
      }
    },
    sfzqmError (err, file) {
      // 上传出错
      this.ruleForm.cardImg = ''
      this.$notify.error({
        title: '错误',
        message: '身份证正面上传出错',
        duration: 6000
      })
    },
    sfzqmSuccess (res, file) {
      // 上传成功
      if (res.errno == 0) {
        this.ruleForm.birthday = res.data.birthday
        this.ruleForm.userCardNum = res.data.idcardNum
        this.ruleForm.userName = res.data.username
        this.ruleForm.gender = res.data.sex == '女' ? 2 : 1
        var Formqm = new FormData()
        Formqm.append('file', file.raw)
        this.$apis
          .baomingcreate(Formqm)
          .then(res => {
            if (res.data.errno == 0) {
              this.ruleForm.cardImg = res.data.data.url
              this.$notify.success({
                title: '成功',
                message: '身份证正面识别成功'
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg,
                duration: 6000
              })
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.$notify.error({
          title: '错误',
          message: res.errmsg,
          duration: 6000
        })
      }
    },
    sfzqmRemove () {
      // 移除图片
      this.ruleForm.cardImg = ''
    },

    sfzhmError (err, file) {
      // 上传出错
      this.ruleForm.cardBackImg = ''
      this.$notify.error({
        title: '错误',
        message: '身份证正面上传出错',
        duration: 6000
      })
    },
    sfzhmSuccess (res, file) {
      // 上传成功
      if (res.errno == 0) {
        var Formqm = new FormData()
        Formqm.append('file', file.raw)
        this.$apis
          .baomingcreate(Formqm)
          .then(res => {
            if (res.data.errno == 0) {
              this.ruleForm.cardBackImg = res.data.data.url
              this.$notify.success({
                title: '成功',
                message: '身份证反面识别成功'
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg,
                duration: 6000
              })
            }
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.$notify.error({
          title: '错误',
          message: res.errmsg,
          duration: 6000
        })
      }
    },
    sfzhmRemove () {
      // 移除图片
      this.cardBackImg.sfzhm = ''
    },
    beforeAvatarUpload (file) {
      // 图片改变
      var isJPG = false
      if (
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/gif'
      ) {
        isJPG = true
      }
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG/GIF 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }

      return isJPG && isLt2M
    },
    realproves (e) {
      // 获取实名信息详情
      if (e) {
        this.$apis.realproves().then(res => {
          this.ruleForm = res.data.data
          this.sfzqmList = [{ url: this.ruleForm.cardImg }]
          this.sfzhmList = [{ url: this.ruleForm.cardBackImg }]
        })
      }
      this.dialogRealyVisible = e
    },
    handleAmend () {
      // 修改实名
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$apis.realUpdate(this.ruleForm).then(res => {
            if (res.data.errno == 0) {
              this.$notify.success({
                title: '成功',
                message: '修改实名认证成功'
              })
              this.getInfo()
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg
              })
            }
            this.loading = false
          })
        } else {
          this.$message({
            message: '请检查数据必填项',
            type: 'warning'
          })
          return false
        }
      })
    },
    handlePublish () {
      // 增加实名

      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$apis.realCreate(this.ruleForm).then(res => {
            if (res.data.errno == 0) {
              this.$notify.success({
                title: '成功',
                message: '实名认证成功'
              })
              this.getInfo()
            } else {
              this.$notify.error({
                title: '错误',
                message: res.data.errmsg
              })
            }
            this.loading = false
          })
        } else {
          this.$message({
            message: '请检查数据必填项',
            type: 'warning'
          })
          return false
        }
      })
    },
    bindProvince (e) {
      this.ruleForm.bankCity = ''
      this.cityListOld.map(item => {
        if (item.code == e) {
          this.cityList = item.children
          this.ruleForm.bankProvince = item.name
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.con-t-select {
  margin-top: 0px;
}
.content {
  display: flex;
  min-height: 100vh;
}
.myorderform {
  flex: 1;
  background-color: #fafafa;
}
.myset {
  text-align: center;
  padding: 70px 20px 50px;
  font-size: 16px;
  img {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    margin-bottom: 10px;
  }
  div {
    span {
      color: #d1483f;
    }
  }
}
.myorderform-title {
  font-size: 18px;
  margin-bottom: 30px;
  text-align: center;
}
.goodsbuy {
  padding: 15px 0px;
  color: #828282;
  font-size: 18px;
  cursor: pointer;
  text-align: center;
}
.goodsbuyatv {
  border-left: 4px solid #d82141;
  background-color: #fff;
}
.orderform {
  background-color: #fff;
  padding: 60px 40px;
  flex: 4;
  box-sizing: border-box;
}
.allorders {
  display: flex;
  margin-bottom: 20px;
}
.cancellation-order {
  border-right: 2px solid #ddd;
  padding: 0px 30px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
}
.cancellation-active {
  color: #d82141;
}
.cancellation-order:last-child {
  border: none;
}
.cancellation-order:first-child {
  padding-left: 0;
}
.uni-input {
  border: 1px solid #e4e4e4;
  width: 30%;
  border-radius: 10px;
  padding: 10px 0;
  padding-left: 20px;
  margin-bottom: 40px;
}
.indent-select {
  display: flex;
  .uni-data-select {
    margin-right: 10px;
  }
  img {
    width: 80px;
    height: 60px;
  }
}
.uni-stat__select {
  margin-right: 40px;
  margin-bottom: 40px;
}
.seedetails {
  color: #fff;
  background: linear-gradient(#d82141, #f36638);
}

.recharge-hd {
  border-left: 3px solid #d82141;
  padding: 2px 10px;
  margin-bottom: 30px;
}
.recharge-bd {
  width: 500px;
  margin-left: 150px;
}

.recharg-but {
  margin-left: 100px;
  margin-top: 20px;
}

.particulars {
  margin: 30px 0px;
  padding: 15px;
  border-top: 1px solid #efefef;
  border-bottom: 1px solid #efefef;
}
.particulars-item {
  display: flex;
  line-height: 30px;
  b {
    color: #444444;
    font-size: 14px;
    flex: 1;
  }
  span {
    color: #888888;
    font-size: 16px;
    flex: 3;
  }
}

.myInfo {
  margin-top: 10px;
  .icon {
    font-style: normal;
    color: #986e32;
    font-size: 12px;
    background: #ffe863;
    border: 1px #986e32 dashed;
    padding: 2px 3px;
    vertical-align: middle;
    cursor: pointer;
    white-space: nowrap;
  }
}
</style>